package com.zkjl.protect_business.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.zkjl.protect_business.common.Consts;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义User
 * </p>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserPrincipal implements UserDetails {
    /**
     * 主键
     */
    private String id;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 真实姓名
     */
    private String realName;

    /**
     * 手机
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;


    /**
     * 状态
     */
    private Boolean ifEnable;

    /**
     * 创建时间
     */
    private Long createTime;

    /**
     * 更新时间
     */
    private Long updateTime;

    /**
     * 用户角色列表
     */
    private String roles;

    /**
     * 用户权限列表
     */
    @JsonIgnore
    private Collection<? extends GrantedAuthority> authorities;

    /**
     * 用户权限列表
     */
    private List<String> permissions;

    /**
     * 所属单位id
     */
    private String orgId;

    /**
     * 所属单位名称
     */
    private String org;

    /**
     * 头像
     */
    private String photo;


    public static UserPrincipal create(User user) {

        String sysRoleName = user.getSysRoleName();

        List<Permission> permissions = user.getPermissions();

        List<GrantedAuthority> authorities = null;
        List<String> collect = null;
        if (CollUtil.isNotEmpty(permissions)) {
             authorities = permissions.stream()
                    .filter(permission -> StrUtil.isNotBlank(permission.getPermission()))
                    .map(permission -> new SimpleGrantedAuthority(permission.getPermission()))
                    .collect(Collectors.toList());

            collect = permissions.stream()
                    .filter(permission -> StrUtil.isNotBlank(permission.getPermission()))
                    .map(permission -> permission.getPermission())
                    .collect(Collectors.toList());
        }


        return new UserPrincipal(user.getId(), user.getUserName(), user.getPassWord(), user.getRealName(), user.getPhone(), user.getEmail(), user.getIfEnable(),
                user.getCreateTime(), user.getUpdateTime(), sysRoleName, authorities, collect, user.getOrgId(), user.getOrg(),user.getPhoto());
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Objects.equals(this.ifEnable, Consts.ENABLE);
    }
}